﻿using System;



using System.Runtime.InteropServices;

using System.Reflection;



namespace msBsc.AppLogic.Cfg
{

    [StructLayout(LayoutKind.Sequential)]

    public struct MSG
    {

        public int hwnd;

        public int message;

        public int wParam;

        public int lParam;

        public int time;

        public POINTAPI pt;

    }



    // 构建消息循环需要的东西  

    static class WinApi
    {

        [DllImport("user32 ", EntryPoint = "PeekMessage")]

        public static extern int PeekMessage(

                        out   MSG lpMsg,

                        int hwnd,

                        int wMsgFilterMin,

                        int wMsgFilterMax,

                        int wRemoveMsg

        );



        [DllImport("user32 ", EntryPoint = "DispatchMessage")]

        public static extern int DispatchMessage(

                      ref   MSG lpMsg

        );



        [DllImport("user32 ", EntryPoint = "TranslateMessage")]

        public static extern int TranslateMessage(

                      ref   MSG lpMsg

        );

    }



    [StructLayout(LayoutKind.Sequential)]

    public struct POINTAPI
    {

        public int x;

        public int y;

    }



    public class SyncWaitor
    {
        //public static void Wait(ref bool flag); 
        // 同步等待，等待ValidFunc的返回值为bool  

        public static void Wait(Delegate ValidFunc, params object[] param)
        {

            // check delegate  

            MethodInfo mi = ValidFunc.Method;

            if (mi.ReturnType != typeof(bool))
            {

                // 这个错误通常情况下在测试期间就会被发现  

                throw new Exception("Valid function must have a return type of bool.");

            }



            // 等着吧  

            while (true)
            {

                // 如果 ValidFunc 验证成功了，那说明我们该返回了  

                bool ret = (bool)ValidFunc.DynamicInvoke(param);

                if (ret == true)
                {

                    break;

                }



                // 执行一条消息  

                MSG msg;



                // 这里只能用PeekMessage，否则GetMessage阻塞在这里可能导致死锁  

                int iRet = WinApi.PeekMessage(out msg, 0, 0, 0, 1);



                // 返回大于0表示收到了消息  

                if (iRet > 0)
                {

                    WinApi.TranslateMessage(ref msg);

                    WinApi.DispatchMessage(ref msg);

                }



            }

        }

    }

}


/*
  

就可以了，当然，你首先得有一个验证等待是否完成的函数。我们把文章前面提到的等待改版一下：

// 随便定义一个新的代理  

public delegate void Delegate0();  

   

// 我的逻辑函数，可能是一系列同步调用的组合  

public void MainLogic()  

{  

      // 一个异步调用，我需要等它完成  

      this.flag = false;   // 我们仍然需要这个标志  

      AsyncObj.AsyncFuncCompleted += this.OnAsyncFuncCompleted;  

      AsyncObj.AsyncFunc();  

   

      SyncWaitor.Wait(new Delegate0(ValidAsyncComplete));  

}  

   

private void OnAsyncFuncCompleted()  

{  

     this.flag = true;  

}  

   

private bool ValidAsyncComplete()  

{  

     return this.flag;  

} 

为了使用方便，我们还可以在SyncWaitor里增加一个Wait的重载版本：

public static void Wait(ref bool flag); 
 */











